package org.biblicalcontent.gbsbijbel.ui.views;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.biblicalcontent.gbsbijbel.IGbsBijbelService;
import org.biblicalcontent.gbsbijbel.model.IBijbelboek;
import org.biblicalcontent.gbsbijbel.model.IHoofdstuk;
import org.biblicalcontent.gbsbijbel.model.ITekst;
import org.biblicalcontent.gbsbijbel.ui.models.StatenvertalingPresentationModel;
import org.biblicalcontent.ui.generiek.ServiceProvider;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eclipse.ui.part.ViewPart;

public class StatenvertalingViewpart extends ViewPart {

	public static final String ID = "org.biblicalcontent.gbsbijbel.ui.views.StatenvertalingViewpart"; //$NON-NLS-1$
	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	private StatenvertalingPresentationModel model = new StatenvertalingPresentationModel();

	private IGbsBijbelService bijbelService;
	private Composite container;

	public StatenvertalingViewpart() {
	}

	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);
		bijbelService = ServiceProvider.getInstance().getService(IGbsBijbelService.class);
	}

	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		container = toolkit.createComposite(parent, SWT.NONE);
		toolkit.paintBordersFor(container);

		TableWrapLayout tableWrapLayout = new TableWrapLayout();
		tableWrapLayout.numColumns = 1;
		container.setLayout(tableWrapLayout);

		createActions();
		initializeToolBar();
		initializeMenu();

		initializeListeners();
	}

	private void initializeListeners() {
		getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(new ISelectionListener() {

			@Override
			public void selectionChanged(IWorkbenchPart part, ISelection selection) {
				TreeSelection treeSelection = (TreeSelection) selection;
				if (!treeSelection.isEmpty()) {
					Object firstElement = treeSelection.getFirstElement();
					if (firstElement instanceof IBijbelboek) {
						IHoofdstuk hoofdstuk = bijbelService.getHoofdstuk((IBijbelboek) firstElement, 1);
						model.setHoofdstuk(hoofdstuk);
					}
				}
			}
		});

		this.model.addPropertyChangeListener(StatenvertalingPresentationModel.PROP_HOOFDSTUK,
				new PropertyChangeListener() {

					@Override
					public void propertyChange(PropertyChangeEvent event) {
						Object newValue = event.getNewValue();
						if (newValue instanceof IHoofdstuk) {
							toonTeksten(((IHoofdstuk) newValue).getTeksten());
						}
					}
				});
	}

	private void toonTeksten(List<ITekst> teksten) {
		for (Control child : container.getChildren()) {
			child.dispose();
		}

		final HyperlinkAdapter adapter = new HyperlinkAdapter() {
			@Override
			public void linkActivated(HyperlinkEvent e) {
				// TODO Auto-generated method stub
				super.linkActivated(e);
			}
		};
		for (ITekst tekst : teksten) {
			FormText formText = toolkit.createFormText(container, true);
			String opmaakTekst = tekst.getOpmaakTekst();
			System.out.println(opmaakTekst);
			formText.setText("<form><p>" + opmaakTekst + "</p></form>", true, false);
			formText.addHyperlinkListener(adapter);

		}
	}

	public void dispose() {
		toolkit.dispose();
		super.dispose();
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager manager = getViewSite().getActionBars().getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

}
